// 格式化菜单数据
export function format(data) {
  const authMap = {};
  const menuArr = [];
  function loop(items, parent) {
    items.forEach(({ children, ...item }) => {
      item.id = `${item.id}`;
      let parentIds = [];
      if (parent) {
        parentIds = [...(parent.parentIds || []), parent.id];
      }
      if (item.path && item.hasAuth) {
        authMap[item.path] = { ...item, parentIds, actions: {} };
      }
      let menu = null;
      // 1: 分组 2: 菜单 3: 按钮
      if (item.type !== 3) {
        if (item.hasAuth) {
          menu = { ...item };
          if (parent) {
            menu.parentIds = parentIds;
            (parent.children || (parent.children = [])).push(menu);
          } else {
            menuArr.push(menu);
          }
        }
      } else if (parent.path && authMap[parent.path] && item.hasAuth) {
        authMap[parent.path].actions[item.code] = { ...item };
      }
      if (children?.length) {
        loop(children, menu);
      }
    });
  }
  loop(data);
  return { authMap, menuArr };
}
